<?php

namespace app\api\controller\shop;


use app\common\controller\Api;
use app\common\model\Area;


/**
 *
 */
class Useraddress extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    protected $app = null;
    protected $appId="wxd286130b28614c62";


    public function addressList(){

        $user = $this->auth->getUser();

        $where = array();
        $keyword = $this->request->param('keyword');
        if($keyword){
            if(is_numeric($keyword)){
                $where['phone'] = array('like','%'.$keyword.'%');
            }else{
                $where['name'] = array('like','%'.$keyword.'%');
            }
        }
        $where['user_id'] = $user["id"];
        $userAddressModel = new \app\admin\model\Useraddress();
        $list = $userAddressModel->where($where)->select();
        $this->success('',$list);
    }

    public function addAddress(){

        $user = $this->auth->getUser();
        $lat = $this->request->param('lat');
        $lng = $this->request->param('lng');
        $username = $this->request->param('username');
        $mobile = $this->request->param('mobile');
        $address = $this->request->param('detail');
        $provinceName = $this->request->param('province');
        $areaName = $this->request->param('area');
        $cityName = $this->request->param('city');

        $areaModel = new Area();
        $province = $areaModel->where('name',$provinceName)->find();
        if($province == null){
            $this->error('城市数据不对，请联系客服');
        }
        $city = $areaModel->where('name',$cityName)->find();


        if($city == null){
            $this->error('城市数据不对，请联系客服');

        }
        $area = $areaModel->where('name',$areaName)->find();

        if($area ==  null){
            $this->error('城市数据不对，请联系客服');

        }


        $userAddressModel = new \app\admin\model\Useraddress();
        $userAddressModel->insert(['name'=>$username,'phone'=>$mobile,'detail'=>$address,
            'user_id'=>$user['id'],'createtime'=>time(),'lat'=>$lat,'lng'=>$lng,
            'province_id'=>$province['id'],'city_id'=>$city['id'],'region_id'=>$area['id'],'province'=>$province['name'],
           'city'=>$city['name'],'area'=>$area['name'] ]);
        $this->success('');

    }

    public function editAddress(){

        $user = $this->auth->getUser();
        $id = $this->request->param('id');
        $lat = $this->request->param('lat');
        $lng = $this->request->param('lng');
        $username = $this->request->param('username');
        $mobile = $this->request->param('mobile');
        $address = $this->request->param('detail');
        $provinceName = $this->request->param('province');
        $areaName = $this->request->param('area');
        $cityName = $this->request->param('city');


        $areaModel = new Area();
        $province = $areaModel->where('name',$provinceName)->find();
        if($province == null){
            $this->error('城市数据不对，请联系客服');
            error_log("========城市数据不对，请联系客服====pid========".json_encode($provinceName));
        }
        $city = $areaModel->where('name',$cityName)->find();


        if($city == null){
            $this->error('城市数据不对，请联系客服');
            error_log("========城市数据不对，请联系客服====cid========".json_encode($cityName));

        }
        $area = $areaModel->where('name',$areaName)->find();

        if($area ==  null){
            $this->error('城市数据不对，请联系客服');
            error_log("========城市数据不对，请联系客服====cid========".json_encode($areaName));

        }

        $userAddressModel = new \app\admin\model\Useraddress();
        $userAddressModel->where('id',$id)->update(['name'=>$username,'phone'=>$mobile,'detail'=>$address,
           'user_id'=>$user['id'],'createtime'=>time(),'lat'=>$lat,'lng'=>$lng,
            'province_id'=>$province['id'],'city_id'=>$city['id'],'region_id'=>$area['id'],'province'=>$province['name'],
            'city'=>$city['name'],'area'=>$area['name'] ]);
        $this->success('');

    }

    public function deleteAddress(){
        $id = $this->request->param('id');
        $user = $this->auth->getUser();
        $userAddressModel = new \app\admin\model\Useraddress();
        $userAddressModel->where(['user_id'=>$user['id'],'id'=>$id])->delete();
        $this->success('');
    }

    public function defaultAddress(){
        $user = $this->auth->getUser();

        $userAddressModel = new \app\admin\model\Useraddress();
        $list = $userAddressModel->order('id','desc')->where('user_id',$user['id'])->find();
        $this->success($user['id'],$list);
    }

}
